package com.ajavaer.framework.web.controller;

import com.ajavaer.framework.common.message.Message;
import com.ajavaer.framework.common.tools.EncryptTools;
import com.ajavaer.framework.core.web.controller.BaseController;
import com.ajavaer.framework.orm.dao.UserFrontDAO;
import com.ajavaer.framework.orm.po.UserFront;
import com.ajavaer.framework.orm.service.UserFrontService;
import com.ajavaer.framework.shiro.ShiroTools;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/UserFront")
public class UserFrontController extends BaseController {
    @Resource
    UserFrontService userFrontService;
    @Resource
    UserFrontDAO userFrontDAO;

    @RequiresPermissions("front:edit")
    @GetMapping("/save")
    public Message save() {
        UserFront userFront = new UserFront();
        userFront.setAccount("liyongyao");
        userFront.setAge(23);
        userFront.setPassword(EncryptTools.createPassword("123456"));
        userFront.setNickname("save");
        userFront = userFrontService.saveOrUpdate(userFront);
        return Message.successResult(userFront);
    }

    @GetMapping("/login")
    public Message login() {
        boolean authenticated = SecurityUtils.getSubject().isAuthenticated();
        if (authenticated) {
            System.out.println("已经登录");
        } else {
            String account = "2";
            String password = "2";
            UsernamePasswordToken token = new UsernamePasswordToken(account, password);
            try {
                SecurityUtils.getSubject().login(token);
            } catch (IncorrectCredentialsException e) {
                return Message.fail("用户名或密码错误");
            } catch (LockedAccountException e) {
                return Message.fail("登录失败，该用户已被冻结");
            } catch (AuthenticationException e) {
                return Message.fail("用户名或密码错误");
            }
        }
        return Message.success("登录成功");
    }

    @GetMapping("/hello")
    public Message hello() {
        return Message.success("hello");
    }

    @RequiresPermissions("front:info")
    @GetMapping("/find")
    public Message find() {
        Page<UserFront> users = userFrontDAO.findAll(PageRequest.of(1, 10));
        return Message.successResult(users);
    }

    @RequiresPermissions("front:delete")
    @GetMapping("/delete")
    public Message delete() {
        return Message.success("删除成功");
    }

    @RequiresPermissions("front:unauth")
    @GetMapping("/unauth")
    public Message unauth() {
        return Message.success();
    }

    @GetMapping("/logout")
    public Message logout() {
        SecurityUtils.getSubject().logout();
        return Message.success();
    }

}
